#include <QKeyEvent>
#include <QtGlobal> // 包含 qrand()
#include <ctime>    // 包含 time()
#include <QPainter>
#include <QTimerEvent>
#include <QPoint>
#include <QList>
#include "mainwindow.h"
#include "ui_mainwindow.h"

#define grid_wh 20



MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent), ui(new Ui::MainWindow) {
    ui->setupUi(this);
   QPoint snakePos(15,15);

    startTimer(100);
    qsrand(static_cast<uint>(time(nullptr))); // 初始化随机数生成器

    dir = 1; // 朝左边

    foodList.push_back(QPoint(1, 1));
    foodList.push_back(QPoint(3, 1));
    foodList.push_back(QPoint(10, 10));
    foodList.push_back(QPoint(13, 15));
    foodList.push_back(QPoint(19, 1));

    snakeBody.push_back(snakePos); // 初始化蛇的身体
}

MainWindow::~MainWindow() {
    delete ui;
}

void MainWindow::paintEvent(QPaintEvent *event) {
    Q_UNUSED(event);
    QPainter painter(this);

    QPen pen(Qt::blue);
    painter.setPen(pen);

    QBrush brush(Qt::green);
    painter.setBrush(brush);

    // 画网格
    for (int i = 0; i < rect().width() / grid_wh; i++) {
        painter.drawLine(QPoint(i * grid_wh, 0), QPoint(i * grid_wh, rect().height()));
    }

    for (int i = 0; i < rect().height() / grid_wh; i++) {
        painter.drawLine(QPoint(0, i * grid_wh), QPoint(rect().width(), i * grid_wh));
    }

    // 画蛇
    for (int i = 0; i < snakeBody.size(); ++i) {
        painter.drawEllipse(snakeBody[i].x() * grid_wh, snakeBody[i].y() * grid_wh, grid_wh, grid_wh);

    }
    QBrush brush1(Qt::red);
    painter.setBrush(brush1);
    // 画食物
    for (const auto &food : foodList) {
        painter.drawRect(food.x() * grid_wh, food.y() * grid_wh, grid_wh, grid_wh);
    }
}

void MainWindow::timerEvent(QTimerEvent *event) {
    Q_UNUSED(event);
    for (int i = snakeBody.size() -1 ; i > 0; --i) {
        snakeBody[i] = snakeBody[i-1];
    }

    switch (dir) {
    case 1: // 左边
        snakeBody[0].setX(snakeBody[0].x() - 1); // 列数-1
        break;
    case 2: // 右边
        snakeBody[0].setX(snakeBody[0].x() + 1); // 列数+1
        break;
    case 3: // 上面
        snakeBody[0].setY(snakeBody[0].y() - 1); // 行数-1
        break;
    case 4: // 下面
        snakeBody[0].setY(snakeBody[0].y() + 1); // 行数+1
        break;
    default:
        break;
    }

    // 检查是否吃到食物
    for (auto it = foodList.begin(); it != foodList.end(); ++it) {

        if ( snakeBody[0] == *it) {
            QPoint newPos = *it ;
            snakeBody.push_back(newPos);
            snakeBody[0] = newPos;    // 更新蛇的位置
            foodList.erase(it); // 移除被吃掉的食物
            generateFood(); // 生成新的食物
            break;
        }
    }
    // 如果蛇的长度超过一定值，移除尾部
    if (snakeBody.size() > 50) {
        snakeBody.pop_back();
    }

    repaint();
}

void MainWindow::keyPressEvent(QKeyEvent *event) {
    if (event->key() == Qt::Key_Left) {
        dir = 1;
    } else if (event->key() == Qt::Key_Right) {
        dir = 2;
    } else if (event->key() == Qt::Key_Up) {
        dir = 3;
    } else if (event->key() == Qt::Key_Down) {
        dir = 4;
    }
}

void MainWindow::generateFood() {
    int x, y;
    do {
        x = qrand() % (rect().width() / grid_wh);
        y = qrand() % (rect().height() / grid_wh);
    } while (foodList.contains(QPoint(x, y)) || snakeBody.contains(QPoint(x, y)));

    foodList.push_back(QPoint(x, y));
}
